#!/bin/sh

# Slackware build script for mupdf

# Originally written by Hubert Hesse (slackbuilds@hubertscorner.de).
# Heavily modified by B. Watson (yalhcru@gmail.com).

# 20170711 bkw:
# - version bump to 1.11.

# 20170621 bkw:
# - fix build with -current's newer openjpeg, BUILD=2.

# 20170122 bkw:
# - version bump to 1.10a.

# 20160725 bkw:
# - version bump to 1.9a.
# - s/i486/i586/g (to match new 14.2 template).
# - script will no longer build old versions of mupdf, too much
#   changed upstream.

# 20150915 bkw BUILD=2:
# - add -DJBIG_NO_MEMENTO, needed for building with jbig2dec-0.12.

# 20150511 bkw:
# - version bump to 1.7a.
# - remembered to reset BUILD to 1, yay.
# - no other changes (amazingly enough).

# 20140827 bkw: fix quality control fails in previous version.
# 2 issues pointed out by Thomas Morper in an email:
# - The source is already found in upstream's archive/, and won't get
#   moved, so update DOWNLOAD in the .info file to point there.
# - Get rid of redundant /usr/share/doc/mupdf dir.
#
# Also, I forgot to reset BUILD to 1 when VERSION changed, so this
# will be build "4" of 1.5 even thought it's really the 2nd one :(

# 20140822 bkw:
# - Updated for mupdf-1.5.
# - Hosting the source on my server, so I know it won't get moved
#   to an archive/ dir when a new release comes out.
# - Script also tested with 1.4:
#   DOWNLOAD="http://urchlay.naptime.net/~urchlay/src/mupdf-1.4-source.tar.gz"
#   MD5SUM="0f6840a7020db0c833b0c090ca1864ec"
# - Add .desktop, .pc, icon
# - Got rid of CVE-2014-2013 patch (integrated by upstream in 1.4).
# - Remove man page patch (not needed for 1.4 and up).
# - Replace openjpeg dep with newly-submitted openjpeg2.
# - Small patch to allow building with openjpeg 2.1.0 (rather than 2.0.0).
# - For now, revert to static libmupdf. Upstream doesn't support
#   shared libs, they hate it when distro packages create them,
#   and rolling my own shared libs is more PITA than it's worth.
# - Upstream renamed the binary to mupdf-x11, add symlinks so scripts
#   or .desktop files can still run it as mupdf, and "man mupdf" will
#   still work.

# 20140224 bkw:
# - backported security fix for CVE-2014-2013. I will be upgrading this
#   build to mupdf-1.3, but it'll take more work than I currently have
#   time for:
#   + mupdf's internals have changed, will have to redo my patches
#   + it depends on openjpeg 2.0 which SBo hasn't got yet. 2.0 uses an
#     incompatible API, so an openjpeg 2.0 SlackBuild would require
#     updates for everything that depends on it.
#   + zathura-pdf-mupdf will need updating (mupdf's API has changed)
#   + probably all the other zathura-* builds will need updating to
#     be compatible with new zathura-pdf-mupdf
#   The security fix is needed now, it's a tiny patch, and shouldn't have
#   to wait on all that other stuff.

# 20140318 bkw: fix permissions on headers and man pages.

# 20130618 bkw:
# - Took over maintenance.
# - Updated for mupdf 1.2.
# - Build library shared, not static.
# - Build with system zlib, libjpeg, openjpeg, jbig2dec, libtiff, libpng,
#   liblcms2 instead of bundled sources (which made the lib + exes *huge*).
#   Also, the included libjpeg is too new (it broke zathura-pdf-mupdf).
# - Include mutool in package (instead of just its man page).
# - Add CHANGES CONTRIBUTORS doc/* to /usr/doc.
# - Use DESTDIR libdir mandir instead of patching makefile.
# - Updated README & slack-desc to mention XPS and CBZ.
# - Fix the man pages slightly.

PRGNAM=mupdf
VERSION=${VERSION:-1.11}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi
set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION-source
tar xvf $CWD/$PRGNAM-$VERSION-source.tar.gz
cd $PRGNAM-$VERSION-source

# don't find/chown/chmod until after the thirdparty junk is removed.

# Build against system libs instead of bundled ones. If we didn't do this,
# libmupdf.a would be useless for building anything else.

# Everything removed from thirdparty/ has to be installed systemwide, and must
# either be a Slackware core pkg, or mentioned in REQUIRES= in the .info
# file... except mujs, which is mupdf's javascript support lib. If missing,
# we end up with a mupdf with no JS support, which is probably a good idea
# for the default build. If you want javascript, MUJS=yes in the env.

MUJS="${MUJS:-no}"

if [ "$MUJS" = "yes" ]; then
  mv thirdparty/mujs .keep
  rm -rf thirdparty/*
  mv .keep thirdparty/mujs
  WITH=with
else
  rm -rf thirdparty/*
  sed -i 's,murun\.o,,' Makefile
  sed -i 's,pdf_drop_js.*,;,' source/pdf/pdf-xref.c
  sed -i '/murun_main/d' source/tools/mutool.c
  WITH=without
fi

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# ref https://bugs.ghostscript.com/show_bug.cgi?id=697175
sed -i '/#define *OPJ_STATIC/d' source/fitz/load-jpx.c

# Hard-code the version number so it'll stop trying to use git to retrieve
# it and spewing harmless but scary 'fatal: Not a git repository' errors.
sed -i "/^VERSION/s,=.*,= $VERSION," Makefile

make verbose=yes \
  XCFLAGS="$SLKCFLAGS -DJBIG_NO_MEMENTO" \
  HAVE_MUJS="$MUJS" \
  build=release \
  prefix=/usr \
  libdir=/usr/lib$LIBDIRSUFFIX \
  mandir=/usr/man \
  docdir=/usr/doc/$PRGNAM-$VERSION \
  DESTDIR=$PKG \
  install

strip $PKG/usr/bin/*

find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

# Compatibility symlinks. Older versions, the binary is just "mupdf".
# Separate -curl binary exists in 1.5 but not 1.4.
ln -s $PRGNAM-x11 $PKG/usr/bin/$PRGNAM
ln -s $PRGNAM.1.gz $PKG/usr/man/man1/$PRGNAM-x11.1.gz
[ -e $PKG/usr/bin/$PRGNAM-x11-curl ] && \
  ln -s $PRGNAM.1.gz $PKG/usr/man/man1/$PRGNAM-x11-curl.1.gz

# mujstest is useless if we don't have JS support. Also libmupdfthird.a
# ends up 8 bytes long & has no object code in it.
[ "$MUJS" = "yes" ] || \
  rm -f $PKG/usr/bin/mujstest \
        $PKG/usr/lib$LIBDIRSUFFIX/libmupdfthird.a

# Installed permissions are atrocious (everything 755).
find $PKG -type f | xargs chmod 644
chmod 755 $PKG/usr/bin/*

# .desktop taken from debian and modified:
# - make it validate.
# - add mime types for cbz and xps.
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

# Icon converted from platform/x11/mupdf.ico, with icotool.
mkdir -p $PKG/usr/share/pixmaps
cat $CWD/$PRGNAM.png > $PKG/usr/share/pixmaps/$PRGNAM.png

# .pc file taken from debian and parameterized.
PCLIBS="-l$PRGNAM -lfreetype -lharfbuzz -ljbig2dec -ljpeg -lcrypto -lopenjp2 -lz -lm"
[ "$MUJS" = "yes" ] && PCLIBS="$PCLIBS -lmupdfthird"

mkdir -p $PKG/usr/lib$LIBDIRSUFFIX/pkgconfig/
sed -e "s,@LIB@,lib$LIBDIRSUFFIX,g" \
    -e "s,@VERSION@,$VERSION,g" \
    -e "s,@PCLIBS@,$PCLIBS,g" \
    $CWD/$PRGNAM.pc > $PKG/usr/lib$LIBDIRSUFFIX/pkgconfig/$PRGNAM.pc

# 'make install' already installed most of the docs.
cp -a CONTRIBUTORS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# slack-desc will conveniently say whether javascript is built in.
mkdir -p $PKG/install
sed "s,@WITH@,$WITH," < $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
